import { Component, OnInit, Output, EventEmitter } from "@angular/core";
import { AdvertisementBean } from "./../bean/advertisement.bean";
import { HttpServcie } from "./../../../service/http.service";
import { Config } from "./../../../service/config";
import { UploadFile, NzNotificationService } from "ng-zorro-antd";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import {InformationBean} from "../../user/information/bean/information.bean";

@Component({
  selector: "advertisement-add-page",
  templateUrl: "advertisement.add.html",
  styleUrls: ["advertisement.add.scss"]
})

export class AdvertisementAdd implements OnInit {
  @Output() outValue = new EventEmitter();
  bean: AdvertisementBean = new AdvertisementBean();
  config: any = Config;
  validateForm: FormGroup;
  nzSpinning: boolean = false; //遮罩

  // 资讯列表
  informList: any = [];
  informationBean: InformationBean = new InformationBean();


  constructor(
    private fb: FormBuilder,
    private httpService: HttpServcie,
    private _notification: NzNotificationService
  ) {}

  ngOnInit() {
    //设置下了框默认只为空
    this.validateForm = this.fb.group({
      discription: [null, [Validators.required]],
      linkUrl:[null,[Validators.nullValidator]],
      infoId:[null,[Validators.nullValidator]]
    });
  }

  submitForm() {
    //没有填的标红
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    //处理是否提交
    for (const i in this.validateForm.controls) {
      if (!this.validateForm.controls[i].valid) {
        return;
      }
    }
    this.nzSpinning = true;
    if (this.fileList.length > 0) {
      let list: any = [];
      this.fileList.forEach(element => {
        list.push(element["response"]["data"]);
      });
      this.bean.url = list.toString();
    }

    this.httpService.advertiseAdd(this.bean).subscribe(data => {
      this.nzSpinning = false;
      if (data["status"] != "200") {
        this._notification.create("error", "错误提示", "新增失败！");
        return;
      }
      this.outValue.emit(true);
    });
  }

  quit() {
    this.outValue.emit(true);
  }

  //文件上传
  previewImage = "";
  previewVisible = false;
  fileList: UploadFile[] = [];

  //查看
  handlePreview = (file: UploadFile) => {
    this.previewImage =
      this.config.IP + this.config.API.getUpload + file["response"]["data"] ||
      file.thumbUrl;
    this.previewVisible = true;
  };

  /**
   * 上传就开始调用
   * @param info
   */
  handleChange(info: { file: UploadFile }) {
    if (info.file.status === "uploading") {
      return;
    }
    if (info.file.status === "done") {
      //设置为接口的查询不然反显有时候没用
      this.fileList.forEach(element => {
        element["thumbUrl"] =
          this.config.IP +
          this.config.API.getUpload +
          element["response"]["data"];
      });
      // Get this url from response in real world.
    }
  }

  //移除文件
  nzRemoveFile = (file: UploadFile) => {
    this.httpService.deleteUpload(file["response"]["data"]).subscribe(
      data => {
        if (data["status"] != "200") {
          this._notification.create("error", "错误提示", "删除失败！");
          return;
        }
        //移除fileList中的数据
        if (this.fileList.length == 1) {
          this.fileList = [];
          return;
        }
        let list: UploadFile[] = [];
        this.fileList.forEach((element, index) => {
          if (element["thumbUrl"] == file.thumbUrl) {
            this.fileList.splice(index, 1);
            this.fileList = this.fileList;
            list = this.fileList;
            this.fileList = [];
          }
        });
        list.forEach(element => {
          this.fileList.push(element);
        });
      },
      error => {
        this._notification.create("error", "错误提示", "请求超时！");
        return;
      }
    );
  };

  /**
   * 获取下拉框选中的值,进行输入框的隐藏显示
   */
  getChange(val) {
    var operateType = val.bean.operateType;
    switch (operateType) {
      // 纯图片展示
      case "1":
        document.getElementById("link").style.display="none";
        document.getElementById("info").style.display="none";
        break;
        // 外部地址
      case "2":
        document.getElementById("link").style.display="";
        document.getElementById("info").style.display="none";
        break;
        // 跳转咨讯详情
      case "3":
        // 获取模态框
        this.informationBean.pageSize = 1000;
        this.informationBean.currentPage = 1;
        this.httpService.getInformationList(this.informationBean).subscribe(data => {
          if (data.status == 200) {
            this.informList = data.data.list;
          }
          console.log(this.informList);
        });
        document.getElementById("link").style.display="none";
        document.getElementById("info").style.display="";
        break;
        // 跳转内部界面
      case "4":
        document.getElementById("link").style.display="";
        document.getElementById("info").style.display="none";
        break;
        // 下载安卓
      case "5":
        document.getElementById("link").style.display="none";
        document.getElementById("info").style.display="none";
        break;
        // 下载iso
      case "6":
        document.getElementById("link").style.display="none";
        document.getElementById("info").style.display="none";
        break;
      default:
        document.getElementById("link").style.display="";
        document.getElementById("info").style.display="";
        break;
    }
  }
}
